package com.ror.domain;

import com.ror.util.DialogUtil;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class Player {
    //玩家类自己实例的集合
    public static final ArrayList<Player> PLAYERS = new ArrayList<>();
    //无人落座时的占位玩家名
    public static final String NAME_PLACEHOLDER = "(虚位以待)";
    //主视角玩家
    public static Player thisPlayer;

    public static void initPlayers() {
        for (Seat seat : Seat.values()) {
            if (!seat.isSittable) continue;
            PLAYERS.add(seat == Seat.MIDDLE ? thisPlayer = new Player(seat) : new Player(seat));
        }
    }

    public static void sortAll() {
        PLAYERS.forEach(Player::sort);
    }

    private String name; //玩家名
    private Seat seat; //玩家座位
    private Role role; //玩家角色

    private Player(Seat seat) {
        this(NAME_PLACEHOLDER, seat, Role.NOT_INIT);
    }

    private Player(String name, Seat seat, Role role) {
        this.name = name;
        this.setSeat(seat);
        this.setRole(role);
    }

    public void sort() {
        this.seat.sortCards();
    }

    public void bid(ActionEvent e) {
        System.out.println("抢地主");

    }

    public void noBid(ActionEvent e) {
        System.out.println("不抢");

    }

    public void play(ActionEvent e) {
        System.out.println("出牌");

    }

    public void pass(ActionEvent e) {
        System.out.println("不要");

    }

    /**
     * 获取名称
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置名称
     *
     * @param name 名称
     */
    public void setName(String name) {
        if (NAME_PLACEHOLDER.equals(name)) {
            DialogUtil.show("起名失败：这是不允许的名字。");
            return;
        }
        this.name = name;
    }

    /**
     * 获取座位
     *
     * @return seat
     */
    public Seat getSeat() {
        return seat;
    }

    /**
     * 设置座位
     *
     * @param seat 座位
     */
    public void setSeat(Seat seat) {
        if (seat.isOccupied()) {
            DialogUtil.show("选座失败：这个座位已经有人了。");
            return;
        }
        this.seat = seat;
        seat.sit();
    }

    /**
     * 获取角色
     *
     * @return role
     */
    public Role getRole() {
        return role;
    }

    /**
     * 设置角色
     *
     * @param role 角色
     */
    public void setRole(Role role) {
        if (role.isFull()) {
            DialogUtil.show("选角失败：这个角色已经满人了。");
            return;
        }
        this.role = role;
        role.count++;
    }

    public ArrayList<Poker> getCards() {
        return this.seat.getCards();
    }

    public String toString() {
        return this.name + "(" + this.role + "," + this.seat + "): "
                + this.getCards() + ", " + this.getCards().size() + "张";
    }
}
